import config from './config';
import tool from './tool';

/**
 * 发起 HTTPS 网络请求
 */
export function request(options) {
  let object = {
    url: config.baseUrl + options.url,
    data: options.data,
    method: options.method || 'GET',
    success(res) {
      let result = res.data;
      if (result.code === 200) {
        options.success && options.success(result);
      } else {
        options.fail && options.fail(result);
      }
    },
    fail(res) {
      console.log('request fail', res);
      options.fail && options.fail(res.data);
    },
    complete(res) {
      options.complete && options.complete(res.data);
    }
  }
  var contentType = options.contentType || 'application/x-www-form-urlencoded';
  var header = {
    'content-type': contentType
  };
  let token = tool.getCache('token');
  if (token) {
    header['Authorization'] = `Bearer ${token}`;
  }
  object.header = header;
  wx.request(object);
}